/**
 * 
 */
package org.seckill.web;

import java.util.Date;
import java.util.List;

import javax.servlet.jsp.PageContext;

import org.seckill.dto.Exposer;
import org.seckill.dto.SeckillExecution;
import org.seckill.dto.SeckillResult;
import org.seckill.entry.Seckill;
import org.seckill.enums.SeckillStateEnum;
import org.seckill.exception.RepeatKillException;
import org.seckill.exception.SeckillCloseException;
import org.seckill.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author yue
 * 2016年6月10日 下午12:12:26
 */
@Controller
@RequestMapping("/seckill")//url：模块/资源/ 
public class SeckillController {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SeckillService seckillService;
	
	/**
	 * 秒杀列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(Model model){
		List<Seckill> seckills = seckillService.getSeckillList();
		model.addAttribute("list", seckills);
		return "list";
	}
	/**
	 * 详情页
	 * @param seckillId
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/{seckillId}/detail",method=RequestMethod.GET)
	public String detail(@PathVariable("seckillId") Long seckillId,Model model){
		if(seckillId == null){
			return "redirect:/seckill/list";
		}
		Seckill seckill = seckillService.getById(seckillId);
		if(seckill == null){
			return "forward:/seckill/list";
		}
		model.addAttribute("seckill", seckill);
		return "detail";
	}
	/**
	 * 暴露秒杀地址
	 * @param seckillId
	 */
	@RequestMapping(value="{seckillId}/exposer",
			method=RequestMethod.POST,
			produces="application/json;charset=UTF-8")
	@ResponseBody
	public SeckillResult<Exposer> exposer(@PathVariable("seckillId") Long seckillId){
		SeckillResult<Exposer> result;
		try {
			Exposer exposer = seckillService.exportSeckillUrl(seckillId);
			result = new SeckillResult<Exposer>(true,exposer);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			result = new SeckillResult<>(false, e.getMessage());
		}
		return result;
	}
	
	/**
	 * 执行秒杀
	 * @param seckillId
	 * @param md5
	 * @param userPhone
	 * @return
	 */
	@RequestMapping(value="/{seckillId}/{md5}/execution",
			method=RequestMethod.POST,
			produces="application/json;charset=UTF-8")
	@ResponseBody
	public SeckillResult<SeckillExecution>  execute(@PathVariable("seckillId") Long seckillId,
																					@PathVariable("md5") String md5,
																					@CookieValue(value="killphone",required=false) Long userPhone){
		SeckillResult<SeckillExecution> result;
		if(userPhone == null){
			return new SeckillResult<SeckillExecution>(false,"未注册");
		}
		try {
			SeckillExecution execution = seckillService.executeSeckill(seckillId, userPhone, md5);
			return new SeckillResult<SeckillExecution>(true,execution);
		} catch (SeckillCloseException e) {
			SeckillExecution execution = new SeckillExecution(seckillId,SeckillStateEnum.END);
			result = new SeckillResult<>(false, execution);
		}catch (RepeatKillException e) {
			SeckillExecution execution = new SeckillExecution(seckillId,SeckillStateEnum.REPEAT_KILL);
			result = new SeckillResult<>(false, execution);
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			SeckillExecution execution = new SeckillExecution(seckillId,SeckillStateEnum.iNNER_ERROR);
			result = new SeckillResult<>(false, execution);
		}
		return result;
	}
	/**
	 * 获取系统时间
	 * @return
	 */
	@RequestMapping(value="/time/now",method=RequestMethod.GET)
	public SeckillResult<Long> time(){
		Date now = new Date();
		return new SeckillResult<Long>(true,now.getTime());
	}
}
